package com.example.demo.controller;


import com.example.demo.entity.Order;
import com.example.demo.entity.Product;
import com.example.demo.service.OrderService;
import com.example.demo.service.ProductService;
import com.example.demo.service.ShoppingCartService;
import com.example.demo.vo.RespBeanEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liuxianchun
 * @date 2021/1/21
 */
@RestController
@RequestMapping(value = "/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private ShoppingCartService shoppingCartService;

    @Autowired
    private ProductService productService;

    @RequestMapping(value = "/getOrder")
    public Map getOrder(@RequestBody Map<String,Object> paramMap){

        Integer userId = (Integer) paramMap.get("userId");
        HashMap<String, Object> map = new HashMap<String, Object>();
        List<Long> Order_Ids = orderService.getOrderIdByUser(userId);  //获取订单id
        if(Order_Ids==null){
            return RespBeanEnum.FIND_ORDER_ERROR.getMap();
        }else{
            List<List<Order>> ordersList = new ArrayList<>();   //需要返回前端的list
            for(Long order_id:Order_Ids){
                List<Order> tempList = new ArrayList<>();
                List<Order> orders = orderService.getOrders(order_id);   //同一笔订单的商品
                for(Order order:orders){
                    Product product = productService.getProductByID(order.getProductId());
                    order.setProductName(product.getProductName());
                    order.setProductPicture(product.getProductPicture());
                    tempList.add(order);
                }
                ordersList.add(tempList);
            }
            map.put("orders",ordersList);
            map.put("code","001");
        }
        return map;
    }

    @RequestMapping(value = "/addOrder")
    public Map addOrder(@RequestBody Map<String,Object> paramMap){

        Integer userId = (Integer) paramMap.get("userId");
        List<Map> products = (List<Map>) paramMap.get("products");
        if(userId==null||products==null){
            return RespBeanEnum.FIND_ORDER_ERROR.getMap();
        }else{
            Long orderId = orderService.getOrderId();
            for (Map<String,Object> product : products) {
                boolean flag = (Boolean)product.get("check");
                if (flag) {  //已勾选的商品
                    int productId = (int)(product.get("productID"));
                    BigDecimal productPrice = new BigDecimal((Integer)product.get("price"));
                    int productNum = (int)(product.get("num"));
                    orderService.addOrder(orderId,userId,productId,productNum,productPrice);  //添加订单
                    shoppingCartService.deleteShoppingCart(userId,productId);   //删除购物车
                }
            }
            return RespBeanEnum.PAY_SUCCESS.getMap();
        }
    }

    @RequestMapping(value = "/orderPay")
    public Map orderPay(@RequestBody Map<String,Object> paramMap){

        Integer userId = (Integer) paramMap.get("userId");
        Long orderId = (Long) paramMap.get("orderId");
        int excute = orderService.orderPay(userId, orderId);
        if(excute>0){
            return RespBeanEnum.PAY_SUCCESS.getMap();
        }else{
            return RespBeanEnum.PAY_ERROR.getMap();
        }
    }

}
